package com.lubase.core.extend.service.impl;

import com.alibaba.fastjson.JSON;
import com.lubase.core.extend.service.AppExtendInfoService;
import com.lubase.core.extend.service.CustomFormServiceAdapter;
import com.lubase.core.extend.service.InvokeMethodAdapter;
import com.lubase.core.extend.service.PageServiceAdapter;
import com.lubase.core.model.AppExtendServiceModel;
import com.lubase.model.DbField;
import com.lubase.orm.extend.service.ColumnRemoteServiceAdapter;
import com.lubase.orm.extend.service.TableTriggerAdapter;
import com.lubase.orm.service.DataAccess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class AppExtendInfoServiceImpl implements AppExtendInfoService {
    @Autowired
    DataAccess dataAccess;

    @Autowired
    TableTriggerAdapter tableTriggerAdapter;
    @Autowired
    ColumnRemoteServiceAdapter columnRemoteServiceAdapter;

    @Autowired
    InvokeMethodAdapter invokeMethodAdapter;

    @Autowired
    CustomFormServiceAdapter customFormServiceAdapter;
    @Autowired
    PageServiceAdapter pageServiceAdapter;

    @Autowired
    CustomFormServiceAdapter formServiceAdapter;

    @Override
    public List<AppExtendServiceModel> getAppExtendInfo() {
        List<AppExtendServiceModel> list = new ArrayList<>();
        // 获取所有的表触发器
        getTableTriggerList(list);
        // 获取所有表单触发器
        getAllFormTrigger(list);
        // 关联字段
        getLookupColumnDataServiceList(list);
        // 获取服务列
        getIColumnRemoteService(list);
        // 获取页面扩展
        getPageDataExtendServiceList(list);
        // 获取表单子表扩展
        getLoadSubTableDataService(list);
        getIGetLeftDataService(list);
        getIGetMainDataService(list);
        getIInvokeMethod(list);
        return list;
    }

    private void getIInvokeMethod(List<AppExtendServiceModel> list) {
        invokeMethodAdapter.getInvokeMethodByIdList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("IInvokeMethod");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("id is :%s, checkRight is :%s", item.getId(), item.checkRight()));
            list.add(model);
        });
    }

    private void getIGetMainDataService(List<AppExtendServiceModel> list) {
        invokeMethodAdapter.getMainDataServiceList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("IGetMainDataService");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("id is :%s, checkRight is :%s", item.getId(), item.checkRight()));
            list.add(model);
        });
    }

    private void getIGetLeftDataService(List<AppExtendServiceModel> list) {
        invokeMethodAdapter.getLeftDataServiceList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("IGetLeftDataService");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("id is :%s, checkRight is :%s", item.getId(), item.checkRight()));
            list.add(model);
        });
    }

    private void getLoadSubTableDataService(List<AppExtendServiceModel> list) {
        formServiceAdapter.getSubTableDataServiceList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("LoadSubTableDataService");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            HashMap<String, String> map = new HashMap<>();
            map.put("id", item.getId());
            map.put("parentTableId", item.getParentTableId());
            map.put("description", item.getDescription());
            map.put("childTableCode", item.getChildTableCode());
            model.setDescription(JSON.toJSONString(map));
            list.add(model);
        });
    }

    private void getIColumnRemoteService(List<AppExtendServiceModel> list) {
        columnRemoteServiceAdapter.getAllService().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("IColumnRemoteService");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("id is %s,tableKey is %s,displayCol is %s", item.getId(), item.tableKey(), item.displayCol()));
            list.add(model);
        });
    }

    private void getTableTriggerList(List<AppExtendServiceModel> list) {
        tableTriggerAdapter.getTableTriggerList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("ITableTrigger");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getTriggerName());
            model.setDescription(String.format("tableCode：%s,add:%s,edit:%s,delete:%s", item.getTriggerTableCode(), item.isAdd(), item.isEdit(), item.isDelete()));
            list.add(model);
        });
    }

    private void getAllFormTrigger(List<AppExtendServiceModel> list) {
        customFormServiceAdapter.getAllFormTrigger().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("IFormTrigger");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("%s", item.getTriggerTableCode()));
            list.add(model);
        });
    }

    private void getPageDataExtendServiceList(List<AppExtendServiceModel> list) {
        pageServiceAdapter.getPageDataExtendServiceList().forEach(item -> {
            AppExtendServiceModel model = new AppExtendServiceModel();
            model.setExtendType("PageDataExtendService");
            model.setClassName(item.getClassName());
            model.setExtendName(item.getDescription());
            model.setDescription(String.format("pageId is :%s", item.getPageId()));
            list.add(model);
        });
    }

    private void getLookupColumnDataServiceList(List<AppExtendServiceModel> list) {
        formServiceAdapter.getLookupColumnDataServiceList().forEach(item -> {
            if (StringUtils.isEmpty(item.getColumnId())) {
                return;
            }
            for (String columnId : item.getColumnId().split(",")) {
                DbField field = getFieldInfo(columnId);
                if (field == null) {
                    continue;
                }
                AppExtendServiceModel model = new AppExtendServiceModel();
                model.setExtendType("LookupColumnDataService");
                model.setClassName(item.getClassName());
                model.setExtendName(item.getDescription());
                model.setDescription(String.format("tableCode is :%s, columnCode is :%s(%s)", field.getTableCode(), field.getName(), field.getCode()));
                list.add(model);
            }
        });
    }

    private DbField getFieldInfo(String columnId) {
        if (StringUtils.isEmpty(columnId)) {
            return null;
        }
        try {
            return dataAccess.getDbFieldByColumnId(Long.parseLong(columnId));
        } catch (Exception exception) {
            log.error("LookupColumnDataService实现 错误，columnId：" + columnId, exception);
            return null;
        }
    }
}
